package com.frank.circle.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frank.circle.entity.ShareCircle;
import com.frank.circle.mapper.ShareCircleMapper;
import com.frank.circle.req.SaveCircleReq;
import com.frank.circle.req.UpdateCircleReq;
import com.frank.circle.service.CircleService;
import com.frank.circle.vo.CircleVo;
import com.frank.common.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2025/10/15
 * @description
 */
@Service
public class CircleServiceImpl implements CircleService {


    private final ShareCircleMapper shareCircleMapper;

    public CircleServiceImpl(ShareCircleMapper shareCircleMapper) {
        this.shareCircleMapper = shareCircleMapper;
    }

    @Override
    public ShareCircle findOne(Long circleId) {
        return shareCircleMapper.selectById(circleId);
    }

    @Override
    public List<CircleVo> listCircle() {
        List<ShareCircle> shareCircles = shareCircleMapper.selectList(Wrappers.<ShareCircle>lambdaQuery());
        List<ShareCircle> list = shareCircles.stream().filter(shareCircle -> shareCircle.getParentId() == -1L).collect(Collectors.toList());
        List<ShareCircle> parentList = shareCircles.stream().filter(shareCircle -> shareCircle.getParentId() != -1L).collect(Collectors.toList());
        return list.stream().map(shareCircle -> {
            CircleVo circleVo = new CircleVo();
            circleVo.setId(shareCircle.getId());
            circleVo.setCircleName(shareCircle.getCircleName());
            circleVo.setIcon(shareCircle.getIcon());
            List<CircleVo> collect = parentList.stream().filter(item -> Objects.equals(item.getParentId(), shareCircle.getId())).map(item -> {
                CircleVo children = new CircleVo();
                children.setId(shareCircle.getId());
                children.setCircleName(shareCircle.getCircleName());
                children.setIcon(shareCircle.getIcon());
                return children;
            }).collect(Collectors.toList());
            circleVo.setChildren(collect);
            return circleVo;
        }).collect(Collectors.toList());
    }

    @Override
    public Boolean saveCircle(SaveCircleReq req) {
        ShareCircle shareCircle = new ShareCircle();
        shareCircle.setCircleName(req.getCircleName())
                .setIcon(req.getIcon())
                .setParentId(req.getParentId())
                .setCreatedTime(new Date())
                .setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int insert = shareCircleMapper.insert(shareCircle);
        return insert > 0;
    }

    @Override
    public Boolean updateCircle(UpdateCircleReq req) {
        ShareCircle shareCircle = new ShareCircle();
        shareCircle
                .setId(req.getId())
                .setCircleName(req.getCircleName())
                .setIcon(req.getIcon())
                .setParentId(req.getParentId())
                .setUpdateTime(new Date());
        int updated = shareCircleMapper.updateById(shareCircle);
        return updated > 0;
    }

    @Override
    public Boolean deleteCircle(Long id) {
        ShareCircle shareCircle = new ShareCircle();
        shareCircle.setId(id);
        shareCircle.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int updated = shareCircleMapper.updateById(shareCircle);
        return updated > 0;
    }
}
